www.gusucode.com > DB-Libray 操作SQLServer编程的VC++ 一例-源码程序 > DB-Libray 操作SQLServer编程的VC++ 一例-源码程序\code\PhManage\DBSession.cpp
// DBSession.cpp: implementation of the CDBSession class. // Download by http://www.NewXing.com ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "PhysicM.h" #include "DBSession.h" #include "dblibrarymsg.h" /* #ifndef DBLIBRARYMSG_H #define DBLIBRARYMSG_H #include "dblibrarymsg.h" #endif*/ #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CDBSession::CDBSession() { SetAutoThrow(true); SetAppName(); SetLogFileName(); SetErrLogFile(); SetOutConsole(); // memset(dbproc_status,0,MAXPROCS); } CDBSession::~CDBSession() { // dbexit(); } bool CDBSession::Connect(char *UserName,char *Password,char *SrvName) { // dberrhandle((DBERRHANDLE_PROC)Err_Handle); dbmsghandle((DBMSGHANDLE_PROC)Msg_Handle); // try{ DBLVer=dbinit(); WriteLog(false,"%s",DBLVer); login=dblogin(); DBSETLUSER(login,UserName); DBSETLPWD(login,Password); DBSETLAPP(login,app_name); BCP_SETL(login,TRUE);//设置批拷贝 dbsetmaxprocs(MAXPROCS); for(int i=0;i<MAXPROCS;i++) { dbproc[i]=dbopen(login,SrvName); if(dbproc[i]==NULL) { char tmp[200]; sprintf(tmp,"severity:%d %s, DBErrCode:%d, DBErrStr:%s 创建连接失败. 服务名:%s", SYSERR.serverity,svrty[SYSERR.serverity],SYSERR.dberr,SYSERR.dberrstr,SrvName); WriteLog(false,tmp); //AfxMessageBox(tmp); /*"severity:%d %s, DBErrCode:%d, DBErrStr:%s 创建连接失败. 服务名:%s", SYSERR.serverity,svrty[SYSERR.serverity],SYSERR.dberr,SYSERR.dberrstr,SrvName); */ /* if(SYSERR.oserr != DBNOERR) { sprintf(tmp,"severity:%d %s,OSErrCode:%d, OSErrStr:%s 服务名:%s", SYSERR.serverity,svrty[SYSERR.serverity],SYSERR.oserr,SYSERR.oserrstr,SrvName); WriteLog(true,tmp); } */ AfxMessageBox(tmp);//ErrCheck(); return FALSE; } dbproc_status[i]=1; } WriteLog(false,"成功登录到%s",SrvName); // dbfreelogin(login); // return dbproc; return TRUE; } void CDBSession::SetAppName(char *AppName) { if(AppName[0]!=0) app_name=AppName; } void CDBSession::SetLogFileName(char *FileName) { if(FileName[0]!=0) LogFileName=FileName; } void CDBSession::SetErrLogFile(char *FileName) { if(FileName[0]!=0) ErrLogFile=FileName; } void CDBSession::SetOutConsole(bool b) { _output_to_console=b; } int CDBSession::SetDataBase(char *DBName) { for(int i=0;i<MAXPROCS;i++) { if(dbuse(dbproc[i],DBName)==FAIL) ErrCheck(ERR_BIND_DATABASE_FAIL,DBName); } return 1; } void CDBSession::ErrCheck(int code,char *str) { char tmp[100]; memset(tmp,0,100); SetErrCode(code); switch(code) { case ERR_NO_FIND_COL: if(str!=NULL) sprintf(tmp,ERR_MSG[-code-100],str); else strcpy(tmp,ERR_MSG[-code-100]); SetErrMsg(tmp); break; case ERR_BCP_BIND_COL: if(str!=NULL) sprintf(tmp,ERR_MSG[-code-100],str); else strcpy(tmp,ERR_MSG[-code-100]); SetErrMsg(tmp); break; case ERR_COLTYPE_NOT_MATCHING: if(str!=NULL) sprintf(tmp,ERR_MSG[-code-100],str); else strcpy(tmp,ERR_MSG[-code-100]); SetErrMsg(tmp); break; case ERR_MAX_SQLSTR: if(str!=NULL) sprintf(tmp,ERR_MSG[-code-100],str); else strcpy(tmp,ERR_MSG[-code-100]); SetErrMsg(tmp); break; case ERR_BIND_DATABASE_FAIL: if(str!=NULL) sprintf(tmp,ERR_MSG[-code-100],str); else strcpy(tmp,ERR_MSG[-code-100]); SetErrMsg(tmp); break; case ERR_NO_FIND_COMPUTEID: if(str!=NULL) sprintf(tmp,ERR_MSG[-code-100],str); else strcpy(tmp,ERR_MSG[-code-100]); SetErrMsg(tmp); break; case ERR_NO_FIND_COMPUTECOLS: if(str!=NULL) sprintf(tmp,ERR_MSG[-code-100],str); else strcpy(tmp,ERR_MSG[-code-100]); SetErrMsg(tmp); break; case ERR_RESULTS_NOROWS: if(str!=NULL) sprintf(tmp,ERR_MSG[-code-100],str); else strcpy(tmp,ERR_MSG[-code-100]); SetErrMsg(tmp); break; case ERR_DBSYS: break; default: strcpy(tmp,ERR_MSG[-code-100]); SetErrMsg(tmp); } if (GetAutoThrow()) throw (*((DBErr *)this)); //throw(*((WOCIError *)this)); } int CDBSession::WriteLog(bool ErrLog,const char *format,...) { va_list vlist; va_start(vlist,format); FILE *lgfp=NULL; if (ErrLog) lgfp=fopen(ErrLogFile,"a+t"); else lgfp=fopen(LogFileName,"a+t"); if (lgfp==NULL) return ErrLog ? -1 : -2; int rt; CTime curtime=CTime::GetCurrentTime(); char tmptime[15]; sprintf(tmptime,"%s",curtime.Format("%Y-%m-%d %H:%M:%S")); fprintf(lgfp,"[%s] ",tmptime); if (_output_to_console) rt=vprintf(format,vlist); rt=vfprintf(lgfp,format,vlist); int fmtlen=strlen(format); if(fmtlen > 1 && format[fmtlen - 1] !='\n') { if(_output_to_console) printf("\n"); fprintf(lgfp,"\n"); } va_end(vlist); fclose(lgfp); return rt; }